package com.dog.basics.service.sys.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.dog.basics.SmsEntity.SmsParameter;
import com.dog.basics.SmsResult.SmsSendResult;
import com.dog.basics.bean.model.common.DogSmsLog;
import com.dog.basics.common.constant.CommonConstants;
import com.dog.basics.common.exception.CustomException;
import com.dog.basics.common.util.R;
import com.dog.basics.common.util.RedisUtil;
import com.dog.basics.security.constants.SecurityConstants;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogSmsLogService;
import com.dog.basics.service.sys.MobileService;
import com.dog.basics.sms.SmsSender;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 手机登录相关业务实现
 */
@Slf4j
@Service
@AllArgsConstructor
public class MobileServiceImpl implements MobileService {

    private final SmsSender smsSender;

    private final IDogSmsLogService ljgSmsLogService;

    /**
     * 发送手机验证码
     * TODO: 调用短信网关发送验证码,测试返回前端
     *
     * @param mobile mobile
     * @return code
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sendSmsCode(String mobile,Long userid) {
        Object codeObj = RedisUtil.get(CommonConstants.DEFAULT_CODE_KEY + "EDIT@" + mobile);
        // 验证码发送频繁
        Validator.validateTrue(ObjectUtil.isNull(codeObj), "验证码发送过频繁");
        // 生成验证码
        String captcha = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
        // 发送验证码
        SmsSendResult send = smsSender.send(new SmsParameter("24118",mobile, captcha));
        // 发送失败校验
        Validator.validateTrue(send.isSuccess(), StrUtil.format("发送失败,错误编码:{}",send.getCode()));
        //保存记录
        DogSmsLog dogSmsLog=new DogSmsLog();
        dogSmsLog.setUserId(userid);
        dogSmsLog.setUserPhone(mobile);
        dogSmsLog.setContent("修改");
        dogSmsLog.setMobileCode(captcha);
        dogSmsLog.setType(1);
        dogSmsLog.setRecDate(DateUtil.currentSeconds());
        dogSmsLog.setResponseCode(send.getCode());
        dogSmsLog.setStatus(1);
        Validator.validateTrue(ljgSmsLogService.save(dogSmsLog),"发送失败!");
        // 保存Redis 中
        RedisUtil.set(CommonConstants.DEFAULT_CODE_KEY + "EDIT@" + mobile, captcha, SecurityConstants.CODE_TIME);
        return Boolean.TRUE;
    }
    /**
     * 手机号注册
     * TODO: 调用短信网关发送验证码,测试返回前端
     *
     * @param mobile mobile
     * @return code
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sendRegisterSms(String mobile) {
        Object codeObj = RedisUtil.get(CommonConstants.DEFAULT_CODE_KEY + "REGISTE@" + mobile);
        // 验证码发送频繁
        Validator.validateTrue(ObjectUtil.isNull(codeObj), "验证码发送过频繁");
        // 生成验证码
        String captcha = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
        // 发送验证码
        SmsSendResult send = smsSender.send(new SmsParameter("24118",mobile,captcha));
        // 发送失败校验
        Validator.validateTrue(send.isSuccess(), StrUtil.format("发送失败,错误编码:{}",send.getCode()));
        //保存记录
        DogSmsLog dogSmsLog=new DogSmsLog();
        dogSmsLog.setUserId(-1L);
        dogSmsLog.setUserPhone(mobile);
        dogSmsLog.setContent("注册");
        dogSmsLog.setMobileCode(captcha);
        dogSmsLog.setType(1);
        dogSmsLog.setRecDate(DateUtil.currentSeconds());
        dogSmsLog.setResponseCode(send.getCode());
        dogSmsLog.setStatus(1);
        Validator.validateTrue(ljgSmsLogService.save(dogSmsLog),"发送失败!");
        // 保存Redis 中
        RedisUtil.set(CommonConstants.DEFAULT_CODE_KEY + "REGISTE@" + mobile, captcha, SecurityConstants.CODE_TIME);
        return Boolean.TRUE;
    }
    /**
     * 绑定
     * TODO: 调用短信网关发送验证码,测试返回前端
     *
     * @param mobile userId
     * @return code
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sendBdSms(String mobile,Long userId) {
        Object codeObj = RedisUtil.get(CommonConstants.DEFAULT_CODE_KEY + "BD@" + mobile);
        // 验证码发送频繁
        Validator.validateTrue(ObjectUtil.isNull(codeObj), "验证码发送过频繁");
        // 生成验证码
        String captcha = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
        // 发送验证码
        SmsSendResult send = smsSender.send(new SmsParameter("24118",mobile, captcha));
        // 发送失败校验
        Validator.validateTrue(send.isSuccess(), StrUtil.format("发送失败,错误编码:{}",send.getCode()));
        //保存记录
        DogSmsLog dogSmsLog=new DogSmsLog();
        dogSmsLog.setUserId(userId);
        dogSmsLog.setUserPhone(mobile);
        dogSmsLog.setContent("绑定");
        dogSmsLog.setMobileCode(captcha);
        dogSmsLog.setType(4);
        dogSmsLog.setRecDate(DateUtil.currentSeconds());
        dogSmsLog.setResponseCode(send.getCode());
        dogSmsLog.setStatus(1);
        Validator.validateTrue(ljgSmsLogService.save(dogSmsLog),"发送失败!");
        // 保存Redis 中
        RedisUtil.set(CommonConstants.DEFAULT_CODE_KEY + "BD@" + mobile, captcha, SecurityConstants.CODE_TIME);
        return Boolean.TRUE;
    }
    /**
     * 登陆发送验证码
     * @param phone
     * @return
     * guonh 2019年12月21日
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sendPhoneSms(String phone,Long userid) {
        Object codeObj = RedisUtil.get(CommonConstants.DEFAULT_CODE_KEY + "SMS@" + phone);
        if (ObjectUtils.isNotNull(codeObj)) {
            throw new CustomException("验证码发送过频繁");
        }
        //生成验证码
        String captcha = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
        SmsSendResult send = smsSender.send(new SmsParameter("24118",phone, captcha));
        if (!send.isSuccess()){
            throw new CustomException(StrUtil.format("发送失败,错误编码:{}",send.getCode()));
        }
        //保存记录
        DogSmsLog dogSmsLog=new DogSmsLog();
        dogSmsLog.setUserId(userid);
        dogSmsLog.setUserPhone(phone);
        dogSmsLog.setContent("验证");
        dogSmsLog.setMobileCode(captcha);
        dogSmsLog.setType(2);
        dogSmsLog.setRecDate(DateUtil.currentSeconds());
        dogSmsLog.setResponseCode(send.getCode());
        dogSmsLog.setStatus(1);
        Validator.validateTrue(ljgSmsLogService.save(dogSmsLog),"发送失败!");
        RedisUtil.set(CommonConstants.DEFAULT_CODE_KEY + "SMS@" + phone, captcha, SecurityConstants.CODE_TIME);
        return true;
    }
    /**
     * 忘记密码发送验证码
     * @param phone
     * @return
     * guonh 2019年12月21日
     */
    @Override
    public Boolean sendforgetSms(String phone, Long userid) {
        Object codeObj = RedisUtil.get(CommonConstants.DEFAULT_CODE_KEY + "FORGET@" + phone);
        if (ObjectUtils.isNotNull(codeObj)) {
            throw new CustomException("验证码发送过频繁");
        }
        //生成验证码
        String captcha = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
        SmsSendResult send = smsSender.send(new SmsParameter("24118",phone, captcha));
        if (!send.isSuccess()){
            throw new CustomException(StrUtil.format("发送失败,错误编码:{}",send.getCode()));
        }
        //保存记录
        DogSmsLog dogSmsLog=new DogSmsLog();
        dogSmsLog.setUserId(userid);
        dogSmsLog.setUserPhone(phone);
        dogSmsLog.setContent("忘记密码");
        dogSmsLog.setMobileCode(captcha);
        dogSmsLog.setType(3);
        dogSmsLog.setRecDate(DateUtil.currentSeconds());
        dogSmsLog.setResponseCode(send.getCode());
        dogSmsLog.setStatus(1);
        Validator.validateTrue(ljgSmsLogService.save(dogSmsLog),"发送失败!");
        RedisUtil.set(CommonConstants.DEFAULT_CODE_KEY + "FORGET@" + phone, captcha, SecurityConstants.CODE_TIME);
        return true;
    }

    /**
     * 订单发送短信
     * @param phone
     * @return
     * guonh 2019年12月21日
     */
    @Override
    public Boolean sendOrderSms(String phone, Long userid) {
        SmsSendResult send = smsSender.send(new SmsParameter("28301",phone, ""));
        if (!send.isSuccess()){
            throw new CustomException(StrUtil.format("发送失败,错误编码:{}",send.getCode()));
        }
        //保存记录
        DogSmsLog dogSmsLog=new DogSmsLog();
        dogSmsLog.setUserId(userid);
        dogSmsLog.setUserPhone(phone);
        dogSmsLog.setContent("订单短信通知");
        dogSmsLog.setMobileCode("");
        dogSmsLog.setType(5);
        dogSmsLog.setRecDate(DateUtil.currentSeconds());
        dogSmsLog.setResponseCode(send.getCode());
        dogSmsLog.setStatus(1);
        Validator.validateTrue(ljgSmsLogService.save(dogSmsLog),"发送失败!");
        return true;
    }

}
